<?php
include dirname(dirname(dirname(__FILE__))).'/include/user-verify.php';
include dirname(dirname(__FILE__)).'/user-manage/functions.php';
include_once dirname(dirname(dirname(__FILE__))).'/include/db_connect.php';
include dirname(__FILE__).'/functions.php';

$cur_id = $_SESSION['user_id'];
$to_exchange_user_id = addslashes($_POST['to-exchange-user']); 
$remark = addslashes($_POST['remark']);
$user = getUserById($to_exchange_user_id);
if($user['exchange_status']=="1"||$user['exchange_status']=="2") {
	echo '<script type="text/javascript">alert("你的换班对象正在处理另外的换班请求，请待其处理完毕后再提交请求！");
		 window.location="exchange-duty.php";</script>';
	exit();
}
$origin_table = getCurDutyTable();
$exchange_table = array();
for($i=1;$i<=7;$i++) {
	foreach(range('a', 'f') as $item) {
		$exchange_table[$i][$item]=addslashes($_POST[$i.'_'.$item]);
		if($exchange_table[$i][$item]!=$origin_table[$i][$item])$exchange_table[$i][$item]=-$exchange_table[$i][$item];
	}
}
$conn = get_db_conn();
$sql = 'update user set exchange_status = 1 where id = '.$cur_id.';';
mysql_query($sql,$conn) or die("db error");
$sql = 'update user set exchange_status = 2 where id = '.$to_exchange_user_id.';';
mysql_query($sql,$conn) or die("db error");
$sql = 'insert into exchange_duty(from_user_id,to_user_id,table_content,add_time,
		status,remark)values ('.$cur_id.','.$to_exchange_user_id.',\''.json_encode($exchange_table).
		'\',"'.date('Y-m-d H:i:s').'","pending","'.$remark.'");';
mysql_query($sql,$conn) or die("db error");
echo '<script type="text/javascript">alert("你的换班请求已发送，请等待对方答复!");
	 window.location="../admin-home.php";</script>';
exit();